package com.simplesoft.accountbook;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class MyDatabaseOpenHelper extends SQLiteOpenHelper {
	private static final int DATABASE_VERSION = 3;
	private static final String ACCOUNT_BOOK_TABLE_NAME = "money_record";
//	private static final String ACCOUNT_BOOK_TABLE_CREATE_V2 = "CREATE TABLE " + 
//			ACCOUNT_BOOK_TABLE_NAME + 
//			" (rid INTEGER PRIMARY KEY AUTOINCREMENT, year INTEGER, month INTEGER, day INTEGER, amount INTEGER);";
	private static final String ACCOUNT_BOOK_TABLE_CREATE_V3 = "CREATE TABLE " +
			ACCOUNT_BOOK_TABLE_NAME + 
			" (rid INTEGER PRIMARY KEY, year INTEGER, month INTEGER, day INTEGER, amount REAL);";
	
	public MyDatabaseOpenHelper (Context context) {
		super(context, "account_book", null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		db.execSQL(ACCOUNT_BOOK_TABLE_CREATE_V3);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		if (oldVersion < 3) {
			db.execSQL("ALTER TABLE money_record RENAME TO money_record_old;");
			db.execSQL("CREATE TABLE money_record (rid INTEGER PRIMARY KEY, year INTEGER, month INTEGER, day INTEGER, amount REAL);");
			db.execSQL("INSERT INTO money_record select rid, year, month, day, amount from money_record_old;");
			db.execSQL("DROP TABLE money_record_old;");
		}
	}

}
